/*************************************************************************
 Copyright (C) Unpublished Unis Software, Inc. All rights reserved.
 Unis Software, Inc., Confidential and Proprietary.

 This software is subject to copyright protection
 under the laws of the Public of China and other countries.

 Unless otherwise explicitly stated, this software is provided
 by Unis "AS IS".
 *************************************************************************/
package com.unis.web.mailfolder;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.beanutils.BeanUtils;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.unis.common.ui.jqGrid.GridUIMapper;
import com.unis.common.util.WebUtil;
import com.unis.model.BaseModel;
import com.unis.model.mailfolder.Mailfolder;
import com.unis.service.mailfolder.MailfolderService;

/**
 * @author Administrator (email:xxh@163.com)
 * @since 2013-02-21
 */
@Controller
@RequestMapping("/mailfolder")
public class MailfolderController {

    private MailfolderService mailfolderService;

    /****************************************
     * Insert a instace of Mailfolder
     ****************************************/
    @RequestMapping("/insert")
    @ResponseBody
    public Object insertMailfolder(@RequestBody Mailfolder mailfolder) throws Exception {
        mailfolder.setStatmentId("Mailfolder.insert");
        String status = "failure";
        mailfolderService.insert(mailfolder);
        status = "success";
        return status;
    }

    /****************************************
     * Update a instace of Mailfolder
     ****************************************/
    @RequestMapping("/update")
    @ResponseBody
    public Object update(@RequestBody Mailfolder mailfolder) throws Exception {
        mailfolder.setStatmentId("Mailfolder.update");
        String status = "failure";
        mailfolderService.update(mailfolder);
        status = "success";
        return status;
    }

    /****************************************
     * Delete a instace of Mailfolder
     ****************************************/
    @RequestMapping("/delete")
    @ResponseBody
    public Object delete(@RequestBody Mailfolder mailfolder) throws Exception {
        mailfolder.setStatmentId("Mailfolder.delete");
        String status = "failure";
        mailfolderService.batchDelete(mailfolder);
        status = "success";
        return status;
    }

    /****************************************
     * Query instaces of Mailfolder
     ****************************************/
    @RequestMapping("/queryAsList")
    @ResponseBody
    public Object queryAsList(HttpServletRequest request) throws Exception {
        Map<String, Object> paramMap = WebUtil.getParamsMapFromRequest(request);
        Mailfolder mailfolder = new Mailfolder();
        mailfolder.setStatmentId("Mailfolder.queryAsList");
        BeanUtils.populate(mailfolder, paramMap);
        Map<String, Object> pageMap = GridUIMapper.process(paramMap);
        BeanUtils.populate(mailfolder, pageMap);
        List<BaseModel> mailfolderList = mailfolderService.queryAsList(mailfolder);
        Map<String, Object> renderMap = GridUIMapper.render(mailfolder, mailfolderList);
        return renderMap;
    }

    /****************************************
     * 查询分类树形结构(一级)
     ****************************************/
    @RequestMapping("/queryFolderLevel")
    @ResponseBody
    public Object queryFolderAsTreeWithOneLayer() throws Exception {
        Mailfolder mailfolder = new Mailfolder();
        mailfolder.setStatmentId("Mailfolder.queryAsList");
        mailfolder.setPaging(false);
        List<BaseModel> mailfolderList = mailfolderService.queryAsList(mailfolder);
        Map<String, Object> root = new HashMap<String, Object>();
        root.put("foldername", "我的邮箱");
        root.put("children", mailfolderList);
        return root;
    }


    /****************************************
     * Query a instaces of Mailfolder
     ****************************************/
    @RequestMapping("/queryAsObject")
    @ResponseBody
    public BaseModel queryAsObject(@RequestBody Mailfolder mailfolder) throws Exception {
        mailfolder.setStatmentId("Mailfolder.queryAsObject");
        return mailfolderService.queryAsObject(mailfolder);
    }

    @RequestMapping("/page")
    public String forword() {
        return "mailfolder/mailfolderList";
    }

    @Resource
    public void setMailfolderService(MailfolderService mailfolderService) {
        this.mailfolderService = mailfolderService;
    }
}
